package org.piaohao.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.piaohao.model.Log;
import org.piaohao.model.User;
import org.piaohao.util.string.UUIDGenerator;

/**
 * Created on : 2013-5-4, 15:46:30
 *
 * @author piaohao
 */
public class MainController extends Controller {

    public static final String moduleName = "登陆登出模块";

    public void login() {
        render("/pages/login/login.jsp");
    }

    public void dologin() {
        HttpSession session = getSession();
        String name = getPara("name");
        String password = getPara("password");
        String captcha = getPara("captcha");
        if (captcha == null || !captcha.equalsIgnoreCase((String) session.getAttribute("captcha"))) {
            setAttr("error", "验证码有误！");
            forwardAction("/main/login");
        } else {
            List<User> users = User.dao.find("select * from t_user where name=?", name);
            if (users == null || users.isEmpty()) {
                setAttr("error", "用户名或密码错误！");
                render("/pages/login/login.jsp");
            } else {
                User user = users.get(0);
                if (user.getStr("password").equals(password)) {
                    getSession().setMaxInactiveInterval(30 * 60);
                    getSession().setAttribute("user", user);
                    String orgId = Db.queryStr("select org_id from t_employee where id=?", user.getStr("emp_id"));
                    if (orgId == null || orgId.equals("")) {
                        orgId = "0";
                    }
                    getSession().setAttribute("orgId", orgId);
                    String empName = Db.queryStr("select e.name from t_user u left join t_employee e on u.emp_id=e.id where u.emp_id=?", user.getStr("emp_id"));
                    if(empName==null||empName.equals("")){
                        empName="系统管理员";
                    }
                    getSession().setAttribute("empName", empName);
                    Log log = new Log();
                    log.set("id", UUIDGenerator.getUUID());
                    log.set("create_date", new Timestamp(new Date().getTime()));
                    log.set("org_id", (String) getSession().getAttribute("orgId"));
                    log.set("content", "[" + moduleName + "][登陆系统][" + empName + "]");
                    log.save();
                    redirect("main");
                } else {
                    setAttr("error", "用户名或密码错误！");
                    render("/pages/login/login.jsp");
                }
            }
        }
    }

    public void loginout() {
        try {
            getSession().removeAttribute("user");
            redirect("login");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void main() {
        render("/pages/main/main.jsp");
    }
}
